
//日历
function Calendar(node) {
    this.init(node);
}
Calendar.prototype = {
    init: function (node) {
        var input = $('.calendar .calendar-input'),
            defaultDate = input.val() != '' ? input.val() : new Date(),   //获取input默认日期
            AllDateInfo = this.getDateInfo(defaultDate)
        this.node = node;                                                 //父级节点
        this.Year = parseInt(AllDateInfo.Year)                            //年
        this.Month = parseInt(AllDateInfo.Month)                          //月
        this.What = parseInt(AllDateInfo.What)                            //日
        this.createTemplate(AllDateInfo);                                 //创建日历模板
        this.addCutYear();
        this.confirmDate();
        this.selecteDate();
        this.open();
        this.hide();
        // 默认值
        $(this.node).find('input').val(this.Year + '-' + this.Month + '-' + this.What);
    },
    //获取日期信息
    getDateInfo: function (defaultDate = new Date()) {
        date = new Date(defaultDate),                               //当前时间
            Year = date.getFullYear(),                                  //当前年份
            Month = date.getMonth() + 1,                                //获取当前月份
            What = date.getDate(),                                      //获取当前几号
            lastWhat = new Date(Year, Month, 0).getDate(),              //当月一共又多少天
            MonthOne = new Date(Year, Month - 1, 1),                    //获取当月一号
            week = MonthOne.getDay();                                   //获取当月一号在星期几
        return {
            date: date,
            Year: Year,
            Month: Month,
            What: What,
            lastWhat: lastWhat,
            MonthOne: MonthOne,
            week: week
        }
    },
    //判断是否是现在日期
    isNow(data1,data2){
        if(data1.Year==data2.Year&&data1.Month==data2.Month){
            return true
        }else{
            return false
        }
    },
    //创建日历模板
    createTemplate: function (dateInfo) {
        
        var template = `<div class="calendar-card p-tb-12 fadeInDown animated" style="display:none">
        <div class="calendar-title flex align-h-center align-v-center">
            <i class=" iconfont icon-zuojiantousuang calendar-icon addYear" type="0" is="year" ></i>
            <i class=" iconfont icon-zuojiantou calendar-icon p-lr-12 addYear" type="0"  is="month"></i>
            <span class="YearMonth">${dateInfo.Year}年${dateInfo.Month}月</span>
            <i class=" iconfont icon-youjiantou calendar-icon p-lr-12 addYear"  type="1" is="month" ></i>
            <i class=" iconfont icon-youjiantousuang calendar-icon addYear" type="1" is="year" ></i>
        </div>
        <div class="calendar-Week flex p-tb-12">
            <div class="font-center flex-2">日</div>
            <div class="font-center flex-2">一</div>
            <div class="font-center flex-2">二</div>
            <div class="font-center flex-2">三</div>
            <div class="font-center flex-2">四</div>
            <div class="font-center flex-2">五</div>
            <div class="font-center flex-2">六</div>
        </div>
        <ul class="flex-wrap flex day-list">
            ${this.RevisePart(dateInfo.lastWhat, dateInfo.week, dateInfo.What)}
        </ul>
        <div class="butt flex p-lr-12 align-h-right m-t-12">
            <div style="width: 50px; " class="m-r-12">
                <button class="button font-14 medium info cancel">取消</button>
            </div>
            <div style="width: 50px; ">
                <button class="button font-14 medium confirm">确认</button>
            </div>
        </div>
        </div>`;
        $(this.node).append(template);
    },
    //设置全局年月
    setYearMonth: function (Year, Month) {
        $('.calendar-card .YearMonth').text(Year + '年' + Month + '月');
        this.Year = Year
        this.Month = Month
    },
    /*修改局部
    @ lastWhat 本月总天数
    @ week     本月一号在周几
    @ What     当天是几号
    */
    RevisePart: function (lastWhat, week, what=0) {
        var nullLi = '',
            Li = '';

        if (week > 0) {
            for (var r = 0; r < week; r++) {
                nullLi += '<li class="font-center flex align-h-center align-v-center  calendar-item"><div class="calendar-block p-8"></div></li>';
            }
        }
        for (var i = 0; i < lastWhat; i++) {
            Li += '<li class="font-center flex align-h-center align-v-center  calendar-item ' + (what == i + 1 ? "calendar-active" : "") + ' "><div class="calendar-block p-8">' + (i + 1) + '</div></li>';
        }
        return nullLi + Li;
    },
    //加减年份
    addCutYear: function () {
        var _this = this;
        $('.calendar-card').on('click', '.addYear', function () {
            var type = parseInt($(this).attr('type')),
                is = $(this).attr('is'),       //用于判断点击的时减月还是年
                year = _this.Year,
                month = _this.Month,
                yearMonth;
            if (is == "month" && month >= 12 && type) { return false; }
            if (is == "month" && month == 1 && !type) {return false; }
            if (is == "year" && year == 1 && !type) { return false; }
            if (type) {
                is == 'year' ? year++ : month++;
                yearMonth = _this.getDateInfo(`${year}-${month}`);
            } else {
                is == 'year' ? year-- : month--;
                yearMonth = _this.getDateInfo(`${year}-${month}`);
            }
            $('.day-list').html('');
            //切换时检测是否是现在日期，是则显示当日几号
            var now = _this.getDateInfo(),
            isnow = _this.isNow(yearMonth,now)
            if(!isnow){
                yearMonth.What = 0
            }else{
                yearMonth.What = now.What
            }
            var LiNode = _this.RevisePart(yearMonth.lastWhat, yearMonth.week, yearMonth.What);
            this.lastWhat = yearMonth.lastWhat;
            _this.setYearMonth(year, month);
            $('.day-list').append(LiNode);
        });
    },
    //确认返回日期
    confirmDate: function () {
        var _this = this;
        $('.calendar-card').on('click', '.confirm', function () {
            var Li = $('.day-list li'),
                count = 0,
                year = _this.Year,
                Month = _this.Month,
                day = 0,
                yearMonth = _this.getDateInfo(`${year}-${Month}`);
            for (var i = 0; i < Li.length; i++) {
                var isClass = Li.eq(i).hasClass("calendar-active");
                if (isClass) {
                    day = Li.eq(i).text();
                    break;
                }
                if (Li.eq(i).text() != '') {
                    count++;
                }
            }
            //判断是否选中某个日期
            if (yearMonth.lastWhat == count) {
                alert('请选择一个日期');
                return false;
            }
            $(_this.node).find('.calendar-input').val(year + '-' + Month + '-' + day);
            $('.calendar-card').hide();
        });
    },
    //选择某个日期
    selecteDate: function () {
        $('.day-list').on('click', 'li', function () {
            $(this).addClass('calendar-active').siblings().removeClass('calendar-active');
        });
    },
    //打开显示日历
    open: function () {
        // calendar-card
        var _this = this
        $(this.node).find('.calendar-input').on('click', function (evnet) {
            evnet.stopPropagation();
            $(document).on('click', _this.documentEvent);
            $('.calendar-card').show();
        });
    },
    //隐藏日历
    hide: function () {
        $(this.node).find('.cancel').on('click', function () {
            $('.calendar-card').hide();
        });
        $('.calendar-card').on('click', function (evnet) {
            evnet.stopPropagation();
        });

    },
    //文档事件处理，点击其他地方后隐藏弹窗
    documentEvent: function (evnet) {
        evnet.stopPropagation();
        $('.calendar-card').hide();
        setTimeout(function () {
            $(document).off('click');
        }, 500)
    }
};